package com.twu.biblioteca.utility;

import com.twu.biblioteca.inputparser.BibliotecaCommandParser;
import com.twu.biblioteca.commandexecutor.BibliotecaCommandExecutor_SubModules;
import com.twu.biblioteca.librarycollection.LibraryCollection;
import com.twu.biblioteca.memberdata.MemberData;
import com.twu.biblioteca.utility.MenuConstants;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Maximilian
 * Date: 19/09/13
 * Time: 1:18 PM
 * To change this template use File | Settings | File Templates.
 */
public class BibliotecaAppIOTester {

    //A class which encapsulates the two objects necessary for testing the BibliothecaApp; first, the app itself, which
    //can be used to add books; second, the output stream which provides the output of the app as a string.
    public static class BibliotecaAppTest
    {
        private final MemberData memberData;
        BibliotecaCommandParser app;
        StringOutputStream oStream;
        LibraryCollection libColl;

        public BibliotecaAppTest(String input)
        {
            InputStream iStream = new ByteArrayInputStream(input.getBytes());
            oStream = new StringOutputStream();
            libColl = new LibraryCollection();
            memberData = new MemberData();
            app = BibliotecaCommandParser.createApp(libColl, memberData, iStream, oStream);
        }

        public BibliotecaCommandParser getApp()
        {
            return app;
        }

        public LibraryCollection getLibraryColl()
        {
            return libColl;
        }

        public MemberData getMemberData()
        {
            return memberData;
        }

        public String run()
        {
            app.run();
            return oStream.getString();
        }
    }

    //Creates a basic app and runs it with the input string; a logout command is appended to ensure that the test
    //doesn't hang (so the sender should not append a logout command)
    public static List <String> generateAppOutputFromString(String in)
    {
        //Just in case the caller forgets, we don't want it to hang
        in += "\n" + MenuConstants.LOGOUT + "\n";

        InputStream iStream = new ByteArrayInputStream(in.getBytes());
        StringOutputStream oStream = new StringOutputStream();

        BibliotecaCommandParser app = BibliotecaCommandParser.createApp(new LibraryCollection(), new MemberData(), iStream, oStream);
        app.run();

        return generateCommandList(oStream.getString());
    }

    //Produces a list containing each non-empty line (delimited by /n) in the input string
    public static List <String> generateCommandList(String str)
    {
        List <String> l = new ArrayList<String>();
        for(String s : str.split("\n"))
        {
            s = s.trim();
            if(!s.equals(""))
            {
                l.add(s);
            }
        }
        return l;
    }
}
